var tools = {
  //轻提示
  showToast(txt) {
    uni.showToast({
      title: txt,
      'icon': 'none'
    })
  },

  //跳转页面
  href(url, options = {}) {
    const { type = 1, ver = false, params = false, time = 0, msg = '', login = '' } = options;

    if (login && !uni.getStorageSync('token')) {
      this.goLogin();
      return false;
    }

    if (ver && !url) {
      this.msg('Empty URL');
      return false;
    }

    if (params) {
      const paramString = new URLSearchParams(params).toString();
      url = new URL(url, window.location.origin);
      url.search = paramString;
      url = url.href;
    }

    if (msg) {
      uni.showToast({
        title: msg,
        icon: 'none',
        mask: true,
        duration: time || 3000
      });
    }

    setTimeout(() => {
      const methods = {
        1: uni.navigateTo,
        2: uni.redirectTo,
        3: uni.reLaunch,
        4: uni.switchTab
      };
      const method = methods[type];
      if (method) {
        method({ url });
      }
    }, time);
  },

  //msg提示
  msg(title, duration = 2000, mask = false, icon = 'none') {
    //统一提示方便全局修改
    if (Boolean(title) === false) {
      return;
    }
    uni.showToast({
      title,
      duration,
      mask,
      icon
    });
  },

  //返回上一级
  pageBack: function (msg, delta = 1, time = 1300) {
    if (msg) {
      this.msg(msg);
    }
    setTimeout(function () {
      uni.navigateBack({
        delta: delta
      });
    }, time);
  },

  //必填验证
  IsEmpty: function (str) {
    return (str == null || str.length < 1) ? false : true;
  },

  //验证正整数
	IsIntegerPositive: function(str) {
		var reg = /^[0-9]*[1-9][0-9]*$/;
		return reg.test(str);
  },

  // 支付
  appPay({ type = 0, data = {} }, successCb, failureCb) {
    const that = this;

    function processPayment(provider, orderInfo) {
      uni.requestPayment({
        provider,
        orderInfo,
        success: () => that.appPaySuccess(successCb),
        fail: () => that.appPayFail(failureCb)
      });
    }

    switch (type) {
      case 2: // 微信支付
        processPayment('wxpay', data);
        break;

      case 1: // 支付宝APP支付
        processPayment('alipay', data);
        break;

      case 3: // 支付宝H5支付
        const isIOS = that.getStorageSyncVal('sysTypes') === 'ios';
        const openURLHandler = () => {
          uni.showToast({
            icon: "none",
            title: "请安装支付宝之后再进行支付！"
          });
        };

        if (isIOS) {
          plus.runtime.launchApplication({ action: data }, openURLHandler);
        } else {
          plus.runtime.openURL(data, openURLHandler);
        }
        break;

      default:
        // 可以添加默认处理逻辑，如提示不支持的支付方式等
        console.error('Unsupported payment type:', type);
        break;
    }
  },

  noToken() {
    uni.removeStorageSync('token');
    uni.removeStorageSync('userInfo');
    uni.removeTabBarBadge({
      index: 3
    })
    uni.hideLoading()
    const platform = uni.getSystemInfoSync().platform
    if (platform != 'ios') {
      uni.reLaunch({
        url: '/pages/login/login'
      });
    } else {
      uni.navigateTo({
        url: '/pages/login/login'
      });
      return;
    }
  },

}

export default tools;
